package com.xtwy.listener;

import com.xtwy.config.Constant;
import com.xtwy.config.ServerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.core.intf.Packet;
import org.tio.websocket.common.WsResponse;
import org.tio.websocket.common.WsSessionContext;
import org.tio.websocket.server.WsServerAioListener;


/**
 * @描述：
 *
 *
 * @公司：  山西迅腾伟业
 * @版本：  1.0.0
 * @日期：  18/08/08
 * @作者：  侯建军
 */
public class ServerAioListener extends WsServerAioListener {
    private static Logger log = LoggerFactory.getLogger(ServerAioListener.class);
    /**
     * 单例对象
     */
    public static final ServerAioListener me = new ServerAioListener();

    /**
     *  连接后
     * @param channelContext
     * @param isConnected
     * @param isReconnect
     * @throws Exception
     */
    @Override
    public void onAfterConnected(ChannelContext channelContext, boolean isConnected, boolean isReconnect) throws Exception {
        super.onAfterConnected(channelContext, isConnected, isReconnect);
        if (log.isInfoEnabled()) {
            log.info("连接后\r\n{}", channelContext);
        }

    }

    /**
     * 数据发送后
     * @param channelContext
     * @param packet
     * @param isSentSuccess
     * @throws Exception
     */
    @Override
    public void onAfterSent(ChannelContext channelContext, Packet packet, boolean isSentSuccess) throws Exception {
        super.onAfterSent(channelContext, packet, isSentSuccess);
        if (log.isInfoEnabled()) {
            log.info("数据发送后\r\n{}\r\n{}", packet.logstr(), channelContext);
        }
    }

    /**
     * 关闭后
     * @param channelContext
     * @param throwable
     * @param remark
     * @param isRemove
     * @throws Exception
     */
    @Override
    public void onBeforeClose(ChannelContext channelContext, Throwable throwable, String remark, boolean isRemove) throws Exception {
        super.onBeforeClose(channelContext, throwable, remark, isRemove);
        if (log.isInfoEnabled()) {
            log.info("连接关闭后\r\n{}", channelContext);
        }
        //会话上下文
        WsSessionContext wsSessionContext = (WsSessionContext) channelContext.getAttribute();

        if (wsSessionContext.isHandshaked()) {

            int count = Tio.getAllChannelContexts(channelContext.groupContext).getObj().size();
            //在线人数统计
            String msg = channelContext.getClientNode().toString() + " 离开了，现在共有【" + count + "】人在线";
            //用tio-websocket，服务器发送到客户端的Packet都是WsResponse
            WsResponse wsResponse = WsResponse.fromText(msg, ServerConfig.CHARSET);
            //群发
            Tio.sendToGroup(channelContext.groupContext, Constant.GROUP_ID, wsResponse);
        }
    }

    /**
     *  数据解码后
     * @param channelContext
     * @param packet
     * @param packetSize
     * @throws Exception
     */
    @Override
    public void onAfterDecoded(ChannelContext channelContext, Packet packet, int packetSize) throws Exception {
        super.onAfterDecoded(channelContext, packet, packetSize);
        if (log.isInfoEnabled()) {
            log.info("数据解码后\r\n{}\r\n{}", packet.logstr(), channelContext);
        }
    }

    /**
     * 数据接受后
     * @param channelContext
     * @param receivedBytes
     * @throws Exception
     */
    @Override
    public void onAfterReceivedBytes(ChannelContext channelContext, int receivedBytes) throws Exception {
        super.onAfterReceivedBytes(channelContext, receivedBytes);
        if (log.isInfoEnabled()) {
            log.info("数据接受后\r\n{}", channelContext);
        }
    }

    /**
     * 数据处理后
     * @param channelContext
     * @param packet
     * @param cost
     * @throws Exception
     */
    @Override
    public void onAfterHandled(ChannelContext channelContext, Packet packet, long cost) throws Exception {
        super.onAfterHandled(channelContext, packet, cost);
        if (log.isInfoEnabled()) {
            log.info("数据处理后\r\n{}\r\n{}", packet.logstr(), channelContext);
        }
    }
}


